using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;


[CreateAssetMenu(fileName = "Effect02-给范围友军加攻击力", menuName = "Skill/Effect/Effect02-给范围友军加攻击力", order = 2)]
public class Effect02 : EffectBase
{
    public int buffCount;
    private Dictionary<Pair, List<Chess>> RecordingChessDic = new();//获得增伤的棋子

    private void OnDestroy()
    {
        RecordingChessDic.Clear();
    }

    public override void Apply(Pair pair, GameEvent e, List<Chess> targets)
    {
        if(targets== null || targets.Count == 0) return;
        var isChant = pair.Chess.chessDate.chessName == "圣歌咏者";
        Chess chess = pair.Chess;
        List<Chess> buffChessList = new List<Chess>();
        if (RecordingChessDic.TryGetValue(pair, out var value))
        {
            buffChessList = value;
        }
        else
        {
            buffChessList = new List<Chess>();
            RecordingChessDic.Add(pair, buffChessList);
        }
        
        if (e is not ChangeEvent changeEvent)
        {
            Debug.LogWarning("传输的GameEvent是错误类型");
            return;
        }

        
        if (changeEvent.ChangeChess == chess)
        {
            foreach (var gameObject in buffChessList)
            {
                gameObject.GetComponent<Chess>().Buff(-buffCount);
                if(isChant) gameObject.GetComponent<Chess>().ChessDetail.ChantBuff--;
            }
            buffChessList.Clear();
            
            var chessList = ChessManager.GetInstance().chessList;
            var friendChess = chessList.Where(c => c.GetChessSide() == chess.GetChessSide()).ToList();
            var targetPositions =
                chess.GetChessSide() == ChessSide.Player1
                    ? chess.chessDate.skillRange.rangeType == RangeType.Normal
                        ? Math.GetTargetPosition(chess.ChessDetail.Position,
                            chess.chessDate.skillRange.vector2IntMode)
                        : chess.chessDate.skillRange.vector2IntMode
                    : chess.chessDate.skillRange.rangeType == RangeType.Normal
                        ? Math.GetEnemyTargetPosition(chess.ChessDetail.Position,
                            chess.chessDate.skillRange.vector2IntMode)
                        : chess.chessDate.skillRange.vector2IntMode;
            var chesses = (from targetChess in friendChess
                where targetPositions.Contains(targetChess.ChessDetail.Position)
                select targetChess).ToList();

            foreach (var c in chesses)
            {
                c.Buff(buffCount);
                if(isChant) c.ChessDetail.ChantBuff++;
                buffChessList.Add(c);
            }
            
        }
        else if (targets.Count != 0)
        {
            if(buffChessList.Contains(changeEvent.ChangeChess)) return;
            changeEvent.ChangeChess.Buff(buffCount);
            if(isChant) changeEvent.ChangeChess.ChessDetail.ChantBuff++;
            buffChessList.Add(changeEvent.ChangeChess);
        }else if (buffChessList.Contains(changeEvent.ChangeChess))
        {
            changeEvent.ChangeChess.Buff(-buffCount);
            if(isChant) changeEvent.ChangeChess.ChessDetail.ChantBuff--;
        }

        RecordingChessDic[pair] = buffChessList;
    }

    public override void RemoveDictionary(Pair pair)
    {
        RecordingChessDic.Remove(pair);
    }
}
